package cn.tj.controller;

import cn.tj.entity.*;
import cn.tj.service.NormalService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/normal")
public class NormalController {//normal普通，普通权限模块的功能

    @Autowired
    private NormalService normalService;

    /**
     * 获取客户列表
     * @return
     */
    @GetMapping("/clientList")
    public Result<List<Client>> ClientList(){
        List<Client> cList=normalService.ClientList();
        return Result.success(cList);
    }

    /**
     * 获取技师列表
     * @return
     */
    @GetMapping("/technicianList")
    public Result<List<Technician>> TechnicianList(){
        List<Technician> tList = normalService.TechnicianList();
        return Result.success(tList);
    }

    /**
     * 获取食物列表
     * @return
     */
    @GetMapping("/foodList")
    public Result<List<Food>> foodList(){
        List<Food> fList = normalService.foodList();
        return Result.success(fList);
    }

    /**
     * 通过id查询房间详情
     * @param map
     * @return
     */
    @PostMapping("/getRoomById")
    public Result<Room> getRoomById(@RequestBody Map<String,Object> map){
        Integer id = (Integer) map.get("id");
        String roomPriceStr = (String) map.get("roomPrice");
        Double roomPrice = Double.parseDouble(roomPriceStr);
        Room room =normalService.getRoomById(id,roomPrice);

        //判断房间是否存在
        if (room!=null){
            return Result.success(room);
        }else{
            return Result.error("当前房间暂未开放");
        }
    }

    /**
     * 开房
     * @param room
     * @return
     */
   @PatchMapping("/openRoom")
    public Result openRoom(@RequestBody Room room){
       normalService.openRoom(room);
       return Result.success();
    }

    /**
     * 退房，对房间信息进行重置
     * @param id
     * @return
     */
    @GetMapping("/offRoom")
    public Result offRoom(Integer id){
       normalService.offRoom(id);
       return Result.success();
    }

    /**
     * 新增食物订单
     * @return
     */
    @PostMapping("/addFoodOrder")
    public Result addFoodOrder(@RequestBody Map<String,Integer> map){
        Integer roomId = map.get("roomId");
        Integer foodId = map.get("foodId");
        Integer foodCount = map.get("foodCount");

        for (int i=1;i<=foodCount;i++){
            //新增食物订单
            normalService.addFoodOrder(roomId,foodId);
        }
        return Result.success();
    }

    /**
     * 结账
     * @param room
     * @return
     */
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Room room){
        //查询用户信息
        Client client = normalService.getClientById(room.getClientId());
        if(client.getMoney()>=room.getMoney()){
            normalService.addOrder(room);
            return Result.success();
        }else {
            return Result.error("余额不足");
        }
    }

    /**
     * 通过房间id查询所下单的食物
     * @param id
     * @return
     */
    @GetMapping("/getFoodsByRid")
    public Result<List<GetFoodsByRid>> getFoodsByRid(Integer id){
        List<GetFoodsByRid> fList = normalService.getFoodsByRid(id);
        return Result.success(fList);
    }

    /**
     * 条件分页查询客户信息
     * @param currentPage 当前页
     * @param pageSize 每页数据条数
     * @param name 客户名称
     * @param vip vip与否
     * @return
     */
    @GetMapping("/clientListByPage")
    public Result<PageBean> ClientListByPage(Integer currentPage,
                                   Integer pageSize,
                                   @RequestParam(required = false)String name,
                                   @RequestParam(required = false)Integer vip

    ){
        PageBean pb =normalService.ClientListByPage(currentPage,pageSize,name,vip);
        return Result.success(pb);
    }

    /**
     * 获取客户的会员与非会员数量
     * @return
     */
    @GetMapping("/getClientVipCount")
    public Result getClientVipCount(){
        Map<String,Object> map = normalService.getClientVipCount();
        return Result.success(map);
    }

    /**
     * 根据id删除客户信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteClientById")
    public Result deleteClientById(Integer id){
        normalService.deleteClientById(id);
        return Result.success();
    }

    /**
     * 通过id查询客户信息
     * @param id
     * @return
     */
    @GetMapping("/getClientById")
    public Result<Client> getClientById(Integer id){
        Client clientById = normalService.getClientById(id);
        return Result.success(clientById);
    }

    /**
     * 通过id对客户信息进行修改or充值
     * @param client
     */
    @PutMapping("/editClientById")
    public Result editClientById(@RequestBody Client client){
        normalService.editClientById(client);
        return Result.success();
    }

    /**
     * 新增用户信息
     * @param client
     * @return
     */
    @PostMapping("/addClient")
    public Result addClient(@RequestBody Client client){
        System.out.println(client);
        normalService.addClient(client);
        return Result.success();
    }

    /**
     * 获取订单列表
     * @return
     */
    @GetMapping("/orderList")
    public Result orderList(){
        List<Order> oList = normalService.orderList();
        return Result.success(oList);
    }

    @GetMapping
    public Result aa(){
        return null;
    }
}
